﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PanelMgr
{

    //面板的父物体
    
    private GameObject ParentObject;
    public Canvas ParentCanvas { private set; get; }
    public Camera UICamera { private set; get; }


    //key:面板类型  value:面板
    private Dictionary<EnumPanelType, PanelBase> panelDict;

    public PanelMgr()
    {
        ParentObject = GameObject.Find("UI");
        ParentCanvas = ParentObject.GetComponentInChildren<Canvas>();
        UICamera = ParentCanvas.GetComponentInChildren<Camera>();
        panelDict = new Dictionary<EnumPanelType, PanelBase>();
    }

    //解析面板
    private PanelBase ParesePanel(EnumPanelType panelType)
    {
        panelDict.TryGetValue(panelType, out PanelBase panel);
        if (panel == null)
        {
            //实例化
            //枚举名字和名字要一样
            GameObject obj = Resources.Load<GameObject>("Prefabs/UI/"+panelType.ToString());
            GameObject p = UnityEngine.Object.Instantiate(obj, ParentCanvas.transform, false);
            panel = p.GetComponent<PanelBase>();
            panelDict.Add(panelType, panel);
            return panel;
        }
        else
            return panel;
    }
    
    
    //显示面板
    public PanelBase ShowPanel(EnumPanelType panelType)
    {
        if(!IsPanelShow(panelType))
        {
            PanelBase showPanel = ParesePanel(panelType);
            showPanel.Show();
            return showPanel;
        }
        else
        {
            panelDict.TryGetValue(panelType, out PanelBase panel);
            return panel;
        }        
    }

    //隐藏面板
    public void HidePanel(EnumPanelType panelType)
    {
        panelDict.TryGetValue(panelType, out PanelBase panel);
        panel.Hide();
    }

    public bool IsPanelShow(EnumPanelType panelType)
    {
        panelDict.TryGetValue(panelType, out PanelBase panel);
        if (panel == null || panel.canvasGroup.alpha == 0)
            return false;
        else
            return true;

    }

    //==============================================
    private static PanelMgr instance;

    public static PanelMgr Instance
    {
        get
        {
            if (instance == null)
                instance = new PanelMgr();
            return instance;
        }
    }
}

public enum EnumPanelType
{
    BagPanel,
    ShortcutPanel,
    SelectPanel,
    GameOverPanel
}